package com.spring.cas.controller;

import com.spring.cas.vo.ResultVO;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description: 主页
 * @author: lfc
 * @version: 2020/7/24 16:12
 */
@RestController
@RequestMapping("/index")
public class IndexController {

    @RequestMapping(value = "/welcome",produces = "application/json; charset=utf-8")
    public ResultVO updateUser()
    {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String userName = null;

        if(authentication.getPrincipal() instanceof UserDetails) {
            userName = ((UserDetails) authentication.getPrincipal()).getUsername();
        }else{
            userName = authentication.getPrincipal().toString();
        }

        return new ResultVO().success("login success welcome " + userName);
    }
}
